<?php

namespace App\Http\Controllers\Wechat;



use App\Model\System\Token;
use App\Model\Wechat\WechatConfig;
use EasyWeChat\Foundation\Application;
use Illuminate\Http\Request;
use App\Http\Controllers\MessagePacket;

class WechatController
{
    protected $app;
    protected $hosturl;

    use MessagePacket;
    /**
     * 获取微信配置信息
     * @return \EasyWeChat\Support\Collection
     *
     */
    public function getWechat()
    {
        try{
            $id = 1;
            $data = WechatConfig::find($id);
            return $this->success($data);

        }catch (\Exception $e){
            return $this->fail('获取失败'.$e->getMessage());
        }

    }

    /**
     * 设置微信配置信息
     * @return \EasyWeChat\Support\Collection
     *
     */
    public function setWechat(Request $request)
    {
        try{
            if(!$request->has(['appid','secret','token'])){
                return $this->fail('参数不全');
            }
            $id = 1;
            $wechat = WechatConfig::find($id);
            if(empty($wechat)){
                ## 新建微信配置对象
                $wechat = new WechatConfig();
                foreach($request->all() as $key => $val){
                    $wechat->$key = $val;
                }
                if(!$request->has('url')){
                    $host = $request->getScheme().'://'.$request->getHttpHost();
                    $wechat->url = $host;
                }
                ## 初始化数据创建微信菜单
                $this->init($wechat);
                $this->createMenu();


                ## 保存微信配置
                $wechat->id = $id;
                $wechat->created_by = Token::user()->userid;
                $wechat->save();
            }else{
                foreach($request->all() as $key => $val){
                    $wechat->$key = $val;
                }
                if(!$request->has('url')){
                    $host = $request->getScheme().'://'.$request->getHttpHost();
                    $wechat->url = $host;
                }
                ## 初始化数据创建微信菜单
                $this->init($wechat);
                $this->createMenu();


                ## 更新微信配置
                $wechat->updated_by = Token::user()->userid;
                $wechat->update($request->all());
            }
            $wechat = WechatConfig::find($id);
            return $this->success($wechat,'保存成功');

        }catch (\Exception $e){
            return $this->fail(false,'保存失败'.$e->getMessage());
        }

    }

    protected function init($wechat)
    {
        $options = [
            'debug'  => true,
            'app_id' => $wechat->appid,
            'secret' => $wechat->secret,
            'token'  => $wechat->token,
            'aes_key' => $wechat->aes_key, // 可选
            'log' => [
                'level' => 'debug',
                'file'  => '/tmp/easywechat.log', // XXX: 绝对路径！！！！
            ],
            //...
        ];

        $this->app = new Application($options);
        $this->hosturl = $wechat->url;
    }

    /**
     * 创建微信公众号菜单
     * @return array
     */

    protected function createMenu()
    {
        $buttons = [
            [
                "name" => "课程产品",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "最新课程",
                        "url"  => $this->hosturl."/courses_m.html"
                    ],
                    [
                        "type" => "view",
                        "name" => "名师介绍",
                        "url"  => $this->hosturl."/teachers_m.html"
                    ]
                ]
            ],
            [
                "type" => "view",
                "name" => "我的课表",
                "url"  => $this->hosturl."/wechat/authorize/schedule"
            ],
            [
                "type" => "view",
                "name" => "个人中心",
                "url"  => $this->hosturl."/wechat/authorize/homepage"
            ]
        ];
        try{
            $this->app->menu->add($buttons);
        }catch (\Exception $e){
            throw new \Exception('创建微信菜单失败');
        }


    }

    protected function deleteMenu($id = null)
    {
        try{
            $result = $this->app->menu->destroy($id);
            if($result['errcode'] == 0){
                return $this->success(true,'删除成功');
            }else{
                return $this->fail(false,'删除失败'.$result['errcode'].$result['errmsg']);
            }
        }catch (\Exception $e){
            return $this->fail(false,'删除菜单失败'.$e->getMessage());
        }

    }

}
